<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app div{
            width: 300px;
            height: 30px;
            border: 1px solid grey;
            margin-top: 5px;
        }
        #app div span{
            padding-right: 20px;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- 2.dom渲染出来 -->
        <!-- 3.双向绑定 -->
        <!-- 4.绑定事件 -->
        <input type="text" name="" id="" placeholder="请输入列表内容" v-model="con"/>
        <button @click="additem">添加</button>
        <div v-for="(item,index) in list">
            <span>{{item.id}}</span> -- <span>{{item.name}}</span> 
            <!-- 6.绑定删除的函数 -->
            <button @click="delitem(index)">x</button>
        </div>
    </div>
    <script src="./js/vue.js"></script>
    <script>
        var app = new Vue({
            el:"#app",
            data:{
                //1.定义变量
                con:"",
                list:[
                    { "id": 1, "name":"吃饭" },
                    { "id": 2, "name":"睡觉" },
                    { "id": 3, "name":"打豆豆" },
                ],
            },
            //5.函数
            methods:{
                additem:function(){
                    var myid = this.list.length;
                    this.list.push({
                        id:myid+1,
                        name:this.con,
                    });
                    //添加后清空input
                    this.con="";
                },
                //7.添加删除的函数
                delitem:function(index){
                    this.list.splice(index,1);
                },
            },
        });
    </script>
</body>
</html>